package com.totoro.microservice.controller;

import com.netflix.hystrix.HystrixCommand;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by Niki on 2018/10/17 19:48
 */
@Slf4j
public class MyHystrixCommand extends HystrixCommand<Response> {
    private String url;

    protected MyHystrixCommand(Setter setter, String url) {
        super(setter);
        this.url = url;
    }

    @Override
    protected Response run() throws Exception {
        Request request = new Request.Builder().url(url).build();

        OkHttpClient client = new OkHttpClient();
        try (Response response = client.newCall(request).execute();) {
            return response;
        }
    }

    @Override
    protected Response getFallback() {
        log.error("调用服务失败，执行默认方法");
//        return super.getFallback();
        return null;
    }
}
